package com.shy.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.shy.base.controller.AbsController;
import com.shy.base.util.CilentUtil;
import com.shy.base.util.LocationUtil;
import com.shy.base.util.WeatherUtil;
import com.shy.base.vo.CityCodeVo;
import com.shy.base.vo.WeatherCodeVo;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping
public class PageController extends AbsController {
	
	
	@SuppressWarnings("unchecked")
	@ModelAttribute
	public void modelAttribute(HttpServletRequest request, Model model) {
		String ip, city;
		Map<String, String> address = new HashMap<>();
		HttpSession session = request.getSession();
		if (session.getAttribute("ip") != null) {
			ip = session.getAttribute("ip").toString();
			address = (Map<String, String>) session.getAttribute("address");
			city = session.getAttribute("city").toString();
		} else {
			Long start1 = System.currentTimeMillis();
			ip = CilentUtil.getClientIP(request);
			//ip = "182.99.94.150";
			address = LocationUtil.getIpAddress(ip);
			city = LocationUtil.getIpAddress(ip).get("city");
			session.setAttribute("ip", ip);
			session.setAttribute("address", address);
			session.setAttribute("city", city);
			Long end1 = System.currentTimeMillis();
			log.error("get ip and location cost:{}", end1 - start1);
		}
		model.addAttribute("ip", ip);
		model.addAttribute("address", address);
		model.addAttribute("city", city);
	}

	
	@RequestMapping({"", "/"})
	public String minsu(Model model) {
		Long start1 = System.currentTimeMillis();
		List<CityCodeVo> cityList = WeatherUtil.cityList;
		String city = (String) model.getAttribute("city");
		//if (StringUtils.isBlank(city) || !WeatherUtil.cityNameMap.containsKey(city)) {
			model.addAttribute("cityList", cityList);
			return "city_list";
		//}
		/*Long end1 = System.currentTimeMillis();
		log.error("city2code cost:{}", end1 - start1);
		return StrUtil.format("redirect:/{}", WeatherUtil.cityNameMap.get(city));*/
	}
	
	
	@RequestMapping("/sitemap.xml")
	public String sitemap(Model model) {
		List<CityCodeVo> cityList = WeatherUtil.cityList;
		model.addAttribute("cityList", cityList);
		return "sitemap";
	}
	
	@RequestMapping("/city")
	public String cityList(Model model) {
		List<CityCodeVo> cityList = WeatherUtil.cityList;
		model.addAttribute("cityList", cityList);
		return "city_list";
	}
	
	@RequestMapping("/list-{nameen:[a-z]+}")
	public String province(@PathVariable("nameen")String nameen, Model model) {
		List<CityCodeVo> cityList = WeatherUtil.cityList;
		for (CityCodeVo cityCodeVo : cityList) {
			if (cityCodeVo.getNameen().equals(nameen)) {
				model.addAttribute("province", cityCodeVo);
				break;
			}
		}
		model.addAttribute("cityList", cityList);
		return "province";
	}
	
	
	@RequestMapping("{nameen:[a-z]+}")
	public String city(@PathVariable("nameen")String nameen, Model model) {
		Long start1 = System.currentTimeMillis();
		if (StringUtils.isBlank(nameen) || !WeatherUtil.cityCodeMap.containsKey(nameen)) {
			return "redirect:/city";
		}
		Map<String, Object> result = WeatherUtil.getWeatherByCode(WeatherUtil.cityCodeMap.get(nameen));
		List<WeatherCodeVo> weatherList = WeatherUtil.weatherList;
		Map<String, WeatherCodeVo> weatherMap = weatherList.stream().collect(Collectors.toMap(WeatherCodeVo::getCode, w -> w));
		Long end1 = System.currentTimeMillis();
		log.error("get weather cost:{}", end1 - start1);
		model.addAttribute("result", result);
		model.addAttribute("weatherMap", weatherMap);
		model.addAttribute("cityList", WeatherUtil.cityList);
		return "city";
	}
}
